package com.conversationboard.batch.jobs.cacherecord;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.conversationboard.cache.BannedUserCache;
import com.conversationboard.cache.RecentMessagesList;
import com.conversationboard.cache.ThreadCache;
import com.conversationboard.cache.ThreadTitleCache;
import com.conversationboard.cache.UserCache;
import com.conversationboard.cache.VoterCache;
import com.conversationboard.cache.banvote.BanVoteCache;
import com.conversationboard.cache.newanswers.INewAnswersCache;
import com.conversationboard.cache.newanswers.NewAnswersCaches;
import com.conversationboard.logger.LogMessageType;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;

public class CacheRecordJob implements Job {

	public void execute(JobExecutionContext context) throws JobExecutionException {

		try {
			
			long startTime = System.currentTimeMillis();
			
			int voterCacheSize = VoterCache.getSizeInKilobytes();
			int userCacheSize = UserCache.getSizeInKilobytes();
			int threadTitleCacheSize = ThreadTitleCache.getSizeInKilobytes();
			int threadCacheSize = ThreadCache.getInstance().getSizeInKilobytes();
			int recentMessageListSize = RecentMessagesList.getSizeInKilobytes();
			int bannedUserCacheSize = BannedUserCache.getSizeInKilobytes();
			int banVoteCacheSize = BanVoteCache.getSizeInKilobytes();

			Map<String, Integer> newAnswersCaches = new HashMap<String, Integer>();

			List<Board> boards = Boards.getAllBoards();

			int totalNewAnswersCacheSize = 0;

			for (Board board : boards) {

				INewAnswersCache cache = NewAnswersCaches.getInstance(board.getBoardId());
				int cacheSize = cache.getSizeInKilobytes();
				totalNewAnswersCacheSize += cacheSize;

				newAnswersCaches.put(board.getName(), cacheSize);
			}

			StringBuilder buffer = new StringBuilder();

			buffer.append("<p><span class=\"red\"><strong>Cache Memory Usage</strong></span><p>");
			
			buffer.append("Voter Cache Size: " + voterCacheSize + "K<br />\n");
			buffer.append("User Cache Size: " + userCacheSize + "K<br />\n");
			buffer.append("Thread Title Cache Size: " + threadTitleCacheSize + "K<br />\n");
			buffer.append("Thread Cache Size: " + threadCacheSize + "K<br />\n");
			buffer.append("Recent Message List Size: " + recentMessageListSize + "K<br />\n");
			buffer.append("Banned User Cache Size: " + bannedUserCacheSize + "K<br />\n");
			buffer.append("Ban Vote Cache Size: " + banVoteCacheSize + "K<br />\n");
			buffer.append("Total New Answer Cache Size: " + totalNewAnswersCacheSize + "K<br />\n");
			
			buffer.append("\n");
			
			for (Map.Entry<String, Integer> entry : newAnswersCaches.entrySet()) {
				buffer.append(entry.getKey() + ": " + entry.getValue() + "K\n");
			}

			Logger.log(new Date(), buffer.toString(), null, LogMessageType.DEBUG);

			long timeTaken = System.currentTimeMillis() - startTime;
			Logger.log(new Date(), "Cache Record Job took: " + timeTaken + " milliseconds", null, LogMessageType.DEBUG);

			
		} catch (SQLException e) {
			throw new JobExecutionException(e);
		}

	}

}
